package net.mcreator.toolsasfood.procedures;

import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.Mirror;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.InteractionHand;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.core.BlockPos;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.CommandSource;

import net.mcreator.toolsasfood.init.ToolsAsFoodModMobEffects;

import java.util.stream.Collectors;
import java.util.List;
import java.util.Comparator;

public class EatingProcedure {
	public static void execute(LevelAccessor world, double x, double y, double z, Entity entity) {
		if (entity == null)
			return;
		String[] tools = {"diamond_shovel", "carrot_on_a_stick", "netherite_axe", "crossbow", "golden_sword", "golden_shovel", "wooden_pickaxe",
				"iron_pickaxe", "bow", "shield", "golden_hoe", "stone_hoe", "netherite_pickaxe", "warped_fungus_on_a_stick", "stone_shovel",
				"diamond_sword", "iron_shovel", "diamond_pickaxe", "iron_axe", "fishing_rod", "wooden_hoe", "golden_axe", "stone_axe", "shears",
				"stone_pickaxe", "trident", "diamond_hoe", "netherite_sword", "wooden_sword", "iron_hoe", "golden_pickaxe", "netherite_shovel",
				"wooden_axe", "spyglass", "netherite_hoe", "stone_sword", "flint_and_steel", "diamond_axe", "wooden_shovel", "iron_sword"};
		String str1 = "" + (entity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY);
		String[] str2 = str1.split(" ");
		String tool = str2[1];
		int j = 0;
		for (int i = 0; i < tools.length; i++) {
			if (tool.equals(tools[i])) {
				j = i;
				break;
			}
		}
		System.out.println(j);
		switch (j) {
			case 0 :
				if (world instanceof ServerLevel _level)
					_level.getServer().getCommands().performCommand(
							new CommandSourceStack(CommandSource.NULL, new Vec3(x, y, z), Vec2.ZERO, _level, 4, "", new TextComponent(""),
									_level.getServer(), null).withSuppressedOutput(),
							"summon item ~ ~ ~ {Item:{id:\"minecraft:diamond\",Count:64b}}");
				break;
			case 1 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(ToolsAsFoodModMobEffects.CARROT_STICK_SUMMON_PIG, 1200, 0));
				break;
			case 2 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(ToolsAsFoodModMobEffects.NETHERITE_AXE_BROKE_LEAVES, 1200, 0));
				break;
			case 3 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(ToolsAsFoodModMobEffects.CROSSBOW_ARROW_ATTACK, 1200, 0));
				break;
			case 4 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(MobEffects.LUCK, 3600, 0));
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(MobEffects.FIRE_RESISTANCE, 3600, 0));
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(MobEffects.DAMAGE_RESISTANCE, 3600, 0));
				break;
			case 5 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(MobEffects.DIG_SPEED, 6000, 0));
				break;
			case 6 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(ToolsAsFoodModMobEffects.WOODEN_PICKAXE_BROKE_COAL, 2400, 0));
				break;
			case 7 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(ToolsAsFoodModMobEffects.IRON_PICKAXE_BROKE, 3600, 0));
				break;
			case 8 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(ToolsAsFoodModMobEffects.BOW_SUMMON_ARROW, 1200, 0));
				break;
			case 9 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(MobEffects.DAMAGE_RESISTANCE, 3600, 0));
				break;
			case 10 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(ToolsAsFoodModMobEffects.GOLDEN_HOE_BROKE_HAY_BALE, 1200, 0));
				break;
			case 11 :
				AteItemsProcedure.execute(world, x, y, z, entity);
				break;
			case 12 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(ToolsAsFoodModMobEffects.NETHERITE_PICKAXE_INGOT, 1200, 0));
				break;
			case 13 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(ToolsAsFoodModMobEffects.WARPED_FUNGUS_STICK_STAND_LAVA, 1200, 0));
				break;
			case 14 :
				SummonTNTProcedure.execute(world, x, y, z);
				break;
			case 15 : {
				final Vec3 _center = new Vec3(x, y, z);
				List<Entity> _entfound = world.getEntitiesOfClass(Entity.class, new AABB(_center, _center).inflate(10 / 2d), e -> true).stream()
						.sorted(Comparator.comparingDouble(_entcnd -> _entcnd.distanceToSqr(_center))).collect(Collectors.toList());
				for (Entity entityiterator : _entfound) {
					if (!(entity == entityiterator)) {
						{
							Entity _ent = entityiterator;
							if (!_ent.level.isClientSide() && _ent.getServer() != null)
								_ent.getServer().getCommands()
										.performCommand(_ent.createCommandSourceStack().withSuppressedOutput().withPermission(4), "kill @s");
						}
					}
				}
			}
				break;
			case 16 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(ToolsAsFoodModMobEffects.IRON_SHOVEL_SUMMON_DIRT, 3600, 0));
				break;
			case 17 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(ToolsAsFoodModMobEffects.DIAMOND_PICKAXE_FIND_ORE, 3600, 0));
				break;
			case 18 :
				if (entity instanceof LivingEntity _livEnt ? _livEnt.hasEffect(MobEffects.DIG_SPEED) : false) {
					if (entity instanceof LivingEntity _entity)
						_entity.addEffect(new MobEffectInstance(MobEffects.DIG_SPEED,
								entity instanceof LivingEntity _livEnt && _livEnt.hasEffect(MobEffects.DIG_SPEED)
										? _livEnt.getEffect(MobEffects.DIG_SPEED).getDuration()
										: 0,
								(int) ((entity instanceof LivingEntity _livEnt && _livEnt.hasEffect(MobEffects.DIG_SPEED)
										? _livEnt.getEffect(MobEffects.DIG_SPEED).getAmplifier()
										: 0) + 1)));
				} else {
					if (entity instanceof LivingEntity _entity)
						_entity.addEffect(new MobEffectInstance(MobEffects.DIG_SPEED, 1200, 0));
				}
				break;
			case 19 : {
				Entity _ent = entity;
				if (!_ent.level.isClientSide() && _ent.getServer() != null)
					_ent.getServer().getCommands().performCommand(_ent.createCommandSourceStack().withSuppressedOutput().withPermission(4),
							"give @s cooked_cod{Enchantments:[{id:\"minecraft:sharpness\",lvl:5}]} 1");
			}
				break;
			case 20 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(ToolsAsFoodModMobEffects.WOODEN_HOE_TP_ITEM, 1200, 0));
				break;
			case 21 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(MobEffects.DIG_SPEED, 18000, 0));
				break;
			case 22 :
				if (world instanceof ServerLevel _serverworld) {
					StructureTemplate template = _serverworld.getStructureManager().getOrCreate(new ResourceLocation("tools_as_food", "stone_tree"));
					if (template != null) {
						template.placeInWorld(_serverworld, new BlockPos((int) (x + -2), (int) y, (int) (z + -2)),
								new BlockPos((int) (x + -2), (int) y, (int) (z + -2)),
								new StructurePlaceSettings().setRotation(Rotation.NONE).setMirror(Mirror.NONE).setIgnoreEntities(false),
								_serverworld.random, 3);
					}
				}
				break;
			case 23 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(ToolsAsFoodModMobEffects.SHEARS_KILL_SHEEPS, 1200, 0));
				break;
			case 24 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(ToolsAsFoodModMobEffects.STONE_PICKAXE_CHAIN, 1200, 0));
				break;
			case 25 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(MobEffects.CONDUIT_POWER, 1200, 0));
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(MobEffects.DOLPHINS_GRACE, 1200, 0));
				break;
			case 26 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(ToolsAsFoodModMobEffects.DIAMOND_HOE_PLOWING, 3600, 0));
				break;
			case 27 :
				NetheritesuitProcedure.execute(entity);
				break;
			case 28 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(MobEffects.DAMAGE_BOOST, 400, 0));
				break;
			case 29 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(ToolsAsFoodModMobEffects.IRON_HOE_JUMP, 1200, 0));
				break;
			case 30 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(MobEffects.DIG_SPEED, 6000, 0));
				break;
			case 31 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(ToolsAsFoodModMobEffects.NETHERITE_SHOVEL_KILL, 1200, 0));
				break;
			case 32 :
				if (world instanceof ServerLevel _serverworld) {
					StructureTemplate template = _serverworld.getStructureManager().getOrCreate(new ResourceLocation("tools_as_food", "tree"));
					if (template != null) {
						template.placeInWorld(_serverworld, new BlockPos((int) (x + -2), (int) y, (int) (z + -2)),
								new BlockPos((int) (x + -2), (int) y, (int) (z + -2)),
								new StructurePlaceSettings().setRotation(Rotation.NONE).setMirror(Mirror.NONE).setIgnoreEntities(false),
								_serverworld.random, 3);
					}
				}
				break;
			case 33 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(MobEffects.NIGHT_VISION, 6000, 0));
				break;
			case 34 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(MobEffects.CONFUSION, 600, 0));
				break;
			case 35 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(MobEffects.DAMAGE_BOOST, 400, 1));
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(MobEffects.REGENERATION, 400, 0));
				break;
			case 36 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(MobEffects.FIRE_RESISTANCE, 3600, 0));
				break;
			case 37 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(MobEffects.MOVEMENT_SPEED, 600, 2));
				if (world instanceof ServerLevel _level)
					_level.getServer().getCommands()
							.performCommand(
									new CommandSourceStack(CommandSource.NULL, new Vec3(x, y, z), Vec2.ZERO, _level, 4, "", new TextComponent(""),
											_level.getServer(), null).withSuppressedOutput(),
									"execute as @e[distance=..10] run effect give @s wither 600 1");
				break;
			case 38 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(ToolsAsFoodModMobEffects.WOODEN_SHOVEL_BROKE_DIRT, 1200, 0));
				break;
			case 39 :
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(MobEffects.DAMAGE_BOOST, 2400, 2));
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(MobEffects.REGENERATION, 2400, 0));
				break;
			default : {
				Entity _ent = entity;
				if (!_ent.level.isClientSide() && _ent.getServer() != null)
					_ent.getServer().getCommands().performCommand(_ent.createCommandSourceStack().withSuppressedOutput().withPermission(4),
							"tellraw @s {\"text\": \"error\"}");
			}
		}
		if (entity instanceof LivingEntity _entity) {
			ItemStack _setstack = new ItemStack(Blocks.AIR);
			_setstack.setCount(1);
			_entity.setItemInHand(InteractionHand.MAIN_HAND, _setstack);
			if (_entity instanceof ServerPlayer _serverPlayer)
				_serverPlayer.getInventory().setChanged();
		}
		if ((entity instanceof Player _plr ? _plr.getFoodData().getFoodLevel() : 0) < 15) {
			if (entity instanceof Player _player)
				_player.getFoodData().setFoodLevel((int) ((entity instanceof Player _plr ? _plr.getFoodData().getFoodLevel() : 0) + 5));
			if (entity instanceof Player _player)
				_player.getFoodData().setSaturation(6);
		}
	}
}
